package com.wangdao.wechatmall.util;

import com.wangdao.wechatmall.bean.pojo.Admin;
import com.wangdao.wechatmall.bean.pojo.AdminExample;
import com.wangdao.wechatmall.bean.pojo.User;
import com.wangdao.wechatmall.bean.pojo.UserExample;
import com.wangdao.wechatmall.mapper.AdminMapper;
import com.wangdao.wechatmall.mapper.UserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName: WXSystemUtils
 * @Description 与系统操作相关的工具类，比如拿到用户信息，和管理员信息
 * @Author Xinhao Yi
 * @Date 2021/7/13  0:16
 *
 * 修改这个utils的名字 防止和系统包重复
 */
public class WXSystemUtils {

    @Autowired
    UserMapper userMapper;

    @Autowired
    AdminMapper adminMapper;

    /**
     * 返回当前用户的userID
     * @return
     */
    public Integer getUserID() {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();

        return userMapper.queryId(username);

    }

    //返回当前使用的用户的信息
    public User systemGetUser(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        User user = users.get(0);
        return user;
    }

    //返回当前使用的管理员的信息
    public Admin systemGetAdmin(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        Admin admin = admins.get(0);
        return admin;
    }
}
